package com.jucai.pppeiyouclient.view.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.CommonAdapter;
import com.jucai.pppeiyouclient.adapter.CommonHolder;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.utils.ActivityCollector;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by UPC on 2017/4/12.
 */

public class AddressSelectSearchActivity extends BaseActivity implements CommonAdapter.AdapterListener {
    private TextView addressTv;
    private EditText addressEt;
    private ImageView deleteIv;
    private PoiSearch poiSearch;
    private RecyclerView recyclerView;
    private CommonAdapter commonAdapter;
    private List<PoiInfo> poiInfoList;

    private String city_name;
    private String city_id;

    private ProgressDialog progressDialog;

    private OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {

        public void onGetPoiResult(PoiResult result) {
            result.getAllPoi();
            progressDialog.dismiss();
            if (result.error != SearchResult.ERRORNO.NO_ERROR) {
                //详情检索失败
                if (poiInfoList != null && poiInfoList.size() != 0) {
                    poiInfoList.clear();
                }
            } else {
                //检索成功
                poiInfoList = result.getAllPoi();
            }
            commonAdapter.setDatas(poiInfoList);
        }

        public void onGetPoiDetailResult(PoiDetailResult result) {

        }

        @Override
        public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

        }
    };

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_addressselectsearch);

        addressTv = (TextView) findViewById(R.id.addressSelectSearch_addressTv);
        addressEt = (EditText) findViewById(R.id.addressSelectSearch_addressEt);
        deleteIv = (ImageView) findViewById(R.id.addressSelectSearch_deleteIv);
        recyclerView = (RecyclerView) findViewById(R.id.addressSelectSearch_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(AddressSelectSearchActivity.this, LinearLayoutManager.VERTICAL, false));

        progressDialog = new ProgressDialog(AddressSelectSearchActivity.this);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在努力检索，请稍后...");

    }

    @Override
    protected void initData() {

        poiInfoList = new ArrayList<>();
        addressTv.setText("");
        commonAdapter = new CommonAdapter(AddressSelectSearchActivity.this, this, poiInfoList);
        recyclerView.setAdapter(commonAdapter);

    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {
        addressTv.setOnClickListener(this);
        deleteIv.setOnClickListener(this);
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setVisibility(View.INVISIBLE);

        addressEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (s.length() == 0) {
                    deleteIv.setVisibility(View.GONE);
                    return;
                }
                deleteIv.setVisibility(View.VISIBLE);
                searchByCityNameAndKeyword(s + "");


                progressDialog.show();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(poiListener);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addressSelectSearch_addressTv:
                Intent intent = new Intent();
                intent.setClass(AddressSelectSearchActivity.this, CitySelectActivity.class);
                intent.putExtra("type", 2);
                startActivityForResult(intent, 1);


                break;

            case R.id.addressSelectSearch_deleteIv:
                addressEt.setText("");

                break;
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_sure:

                break;
            default:

                break;

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
            city_name = data.getStringExtra("city_name");
            city_id = data.getStringExtra("city_id");
            addressTv.setText(city_name);
            searchByCityNameAndKeyword("");
        }
    }

    @Override
    public boolean getCache() {
        return true;
    }

    @Override
    public int getItemLayoutId() {
        return R.layout.item_addresssearch;
    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(View view) {
        return new CommonHolder(view);
    }

    @Override
    public void initItemData(RecyclerView.ViewHolder holder, int position) {
        final PoiInfo poiInfo = poiInfoList.get(position);
        CommonHolder commonHolder = (CommonHolder) holder;
        commonHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("latitude", poiInfo.location.latitude);
                intent.putExtra("longitude", poiInfo.location.longitude);
                intent.putExtra("address", poiInfo.address);
                intent.putExtra("city", poiInfo.city);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        ((TextView) commonHolder.getView(R.id.addressSearch_nameTv)).setText(poiInfo.name);
        ((TextView) commonHolder.getView(R.id.addressSearch_addressTv)).setText(poiInfo.address);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void searchByCityNameAndKeyword(String keyword) {
        poiSearch.searchInCity((new PoiCitySearchOption())
                .city(addressTv.getText().toString().trim())
                .keyword(keyword)
                .pageNum(10));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        poiSearch.destroy();

    }

}
